﻿using Microsoft.SharePoint;
using NGPT.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace NGPT.ExportToGephi
{
    /// <summary>
    /// Implement export form presenter.
    /// </summary>
    public class ExportPresenter
    {
        private const string DEFAULT_NON_TECHNICAL_ERROR_MESSAGE = "Unexpected error occured. See details in the ULS.";

        IExportView view;
        IExportService service;

        /// <summary>
        /// Implement export form presenter.
        /// </summary>
        /// <param name="view">Export form view instance.</param>
        /// <param name="service">Export service instance.</param>
        public ExportPresenter(IExportView view, IExportService service)
        {
            this.view = view;
            this.service = service;
        }

        /// <summary>
        /// Implement export UI command.
        /// </summary>
        public void Export()
        {
            view.ErrorMessage = string.Empty;

            try
            {
                MemoryStream ms = new MemoryStream();

                service.ExportToGEXF(ms, view.IncludePeople, view.includePeopleExpertiseTags, view.IncludeCommunities, view.IncludeSpheres, view.IncludeDiscussions);

                ms.Flush();
                ms.Seek(0, SeekOrigin.Begin);
                view.ExportToStream = ms;
            }
            catch (Exception ex)
            {
                view.ErrorMessage = DEFAULT_NON_TECHNICAL_ERROR_MESSAGE;
                Logger.LogError(ex);
            }
        }
    }
}
